package com.stone.mythread.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// 第一步，创建资源类
public class ThreadMain3 {

    // 定义标志位
    private int flag = 1; // 1 AA 线程， 2 BB 线程， 3 CC 线程

    // 创建 Lock 锁
    private Lock lock = new ReentrantLock();

    // 创建三个condition
    Condition c1 = lock.newCondition();
    Condition c2 = lock.newCondition();
    Condition c3 = lock.newCondition();

    // 打印5次，参数第几轮
    public void print5(int loop) throws InterruptedException {
        // 上锁
        lock.lock();
        try {
            while (flag != 1) {
                // 等待
                c1.await();
            }
            // 干活
            for (int i = 1; i <= 5; i++) {
                System.err.println(Thread.currentThread().getName() + " :: " + i + " :: 轮数 ： " + loop);
            }

            // 通知, 修改 flag = 2 ，在通知 BB 线程
            flag = 2;
            c2.signal();
        } finally {
            // 释放锁
            lock.unlock();
        }
    }


    // 打印10次，参数第几轮
    public void print10(int loop) throws InterruptedException {
        // 上锁
        lock.lock();
        try {
            while (flag != 2) {
                // 等待
                c2.await();
            }
            // 干活
            for (int i = 1; i <= 10; i++) {
                System.err.println(Thread.currentThread().getName() + " :: " + i + " :: 轮数 ： " + loop);
            }

            // 通知, 修改 flag = 3 ，在通知 CC 线程
            flag = 3;
            c3.signal();
        } finally {
            // 释放锁
            lock.unlock();
        }
    }

    // 打印15次，参数第几轮
    public void print15(int loop) throws InterruptedException {
        // 上锁
        lock.lock();
        try {
            while (flag != 3) {
                // 等待
                c3.await();
            }
            // 干活
            for (int i = 1; i <= 15; i++) {
                System.err.println(Thread.currentThread().getName() + " :: " + i + " :: 轮数 ： " + loop);
            }

            // 通知, 修改 flag = 1 ，在通知 AA 线程
            flag = 1;
            c1.signal();
        } finally {
            // 释放锁
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        ThreadMain3 san = new ThreadMain3();
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    san.print5(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    san.print10(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "BB").start();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    san.print15(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "CC").start();
    }

}
